/*
 * Created on Feb 25, 2005
 */
package com.scalar.chiptrack.finance.openpo.actioncommands;

import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackExtendedAction;

import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItem;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemList;
import com.scalar.chiptrack.finance.openpo.ReceivingLineItem;
import com.scalar.chiptrack.finance.openpo.ReceivingLineItemList;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;
import com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderForm;
import com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderReceiptForm;
import com.scalar.chiptrack.operations.vendor.Vendor;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;

/**
 * @author Ravikanth
**/
public class PurchaseOrderReceiptNewCommand extends ChipTrackExtendedAction
{
	/* 
	 * Create a New Receipt
	 */
	public ActionForward executeRequest( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception
	{

		HashMap errors              = new HashMap();
		HashMap info                = new HashMap();
		PurchaseOrderReceiptForm purchaseOrderReceiptForm = ( PurchaseOrderReceiptForm ) form;
		String actionForward 		= "receiptSearch";
		HttpSession mSession 		= request.getSession();
		String receiptNumber        = "";
		Date date                   = new Date();
		PurchaseOrderLineItemList poLineItemList = new PurchaseOrderLineItemList();
		
		try
		{
			resetForm( purchaseOrderReceiptForm );
			receiptNumber = PurchaseOrderDAO.loadAutomatedReceiptNo();

			purchaseOrderReceiptForm.setReceipt_Number( receiptNumber ) ;
			purchaseOrderReceiptForm.setReceiptDate( StringUtils.formatDateToString( date, "MM/dd/yyyy" ) );

			if( purchaseOrderReceiptForm.getPurchaseOrder_Id() != -1 )
			{
				PurchaseOrderForm purchaseOrder = new PurchaseOrderForm();
				purchaseOrder.setPurchaseOrder_Id( purchaseOrderReceiptForm.getPurchaseOrder_Id() );
				PurchaseOrderDAO.loadPurchaseOrder( purchaseOrder );
				purchaseOrderReceiptForm.setPurchaseOrderNumber( purchaseOrder.getPurchaseOrderNumber() );
                purchaseOrderReceiptForm.setPoMaxAmt ( purchaseOrder.getPoMaxAmt() );
                purchaseOrderReceiptForm.setPrevReceiptsTotal( purchaseOrder.getPrevReceiptsTotal() );
                purchaseOrderReceiptForm.setVendor_id( purchaseOrder.getVendor_id() );
                purchaseOrderReceiptForm.setVendorDocNumber( purchaseOrder.getVendorQuoteNumber() );
				Vendor vendor = VendorDAO.getVendorDetails( purchaseOrder.getVendor_id() );
				purchaseOrderReceiptForm.setVendorName( vendor.getVendorName() );
                purchaseOrderReceiptForm.setPoTaxType( purchaseOrder.getPoTaxType() );
                purchaseOrderReceiptForm.setPoType( purchaseOrder.getPoType() );

                poLineItemList = purchaseOrder.getPurchaseOrderLineItemList();
				
				if( poLineItemList != null && poLineItemList.isEmpty() == false )
				{
					setReceiptLineItems( purchaseOrder, purchaseOrderReceiptForm );
				}
                else
                {
                    purchaseOrderReceiptForm.setReceivingLineItemList( null );
                }
                purchaseOrderReceiptForm.setOpenPOLotList( null );
                request.setAttribute("poTaxTypes", PurchaseOrderDAO.loadPOTaxesList() );
			}
			else
			{
				errors.put( "RECEVING TRANSACTION COULD NOT BE LOADED", null );
			}
                
			actionForward = "receipt";
		}
		catch ( Exception e )
		{
            e.printStackTrace();
            Logger.error( ERROR_LOADING_RECEIVINGS_TRANSACTION_INFORMATION, e );
			errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_LOADING_RECEIVINGS_TRANSACTION_INFORMATION );
			mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( ERROR_LOADING_RECEIVINGS_TRANSACTION_INFORMATION, e ) );
			request.setAttribute( "vendorsList", PurchaseOrderDAO.loadAllVendors() );
			request.setAttribute( "yearList", PurchaseOrderDAO.getYearList () );
			request.setAttribute( "productList", PurchaseOrderDAO.loadPOProductsList() );
			actionForward = "receiptSearch";
		}

		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
		return mapping.findForward( actionForward );
    }

	/**
	 * @param purchaseOrderReceiptForm
	 */
	private void resetForm(PurchaseOrderReceiptForm purchaseOrderReceiptForm)
	{
		purchaseOrderReceiptForm.setReceipt_id( -1 );
		purchaseOrderReceiptForm.setSub_total( 0.0 );
		purchaseOrderReceiptForm.setFrieght( 0.0 ); 
		purchaseOrderReceiptForm.setTrade_Discount( 0.0 );
		purchaseOrderReceiptForm.setTax( 0.0 );
		purchaseOrderReceiptForm.setMiscellaneous( 0.0 );
		purchaseOrderReceiptForm.setTotal( 0.0 );
        purchaseOrderReceiptForm.setPoTaxType( null);
        purchaseOrderReceiptForm.setInvoiceNumber( null );
    }

	/**
	 * Set all the Open Purchase Order Line Items To the Receiving Line Items
	 * @param purchaseOrder
	 * @param purchaseOrderReceiptForm
	 */
	private void setReceiptLineItems(PurchaseOrderForm purchaseOrder, PurchaseOrderReceiptForm purchaseOrderReceiptForm)
	{
		PurchaseOrderLineItemList poLineItemList 	= purchaseOrder.getPurchaseOrderLineItemList();
		ReceivingLineItemList lineItemList 			= new ReceivingLineItemList();
		PurchaseOrderLineItem poLineItem			= null;
		ReceivingLineItem recevingLineItem			= null;

		for( int i = 0; i < poLineItemList.size(); i++ )
		{
			poLineItem = (PurchaseOrderLineItem)poLineItemList.get( i );
			
			if( !PURCHASE_ORDER_STATUS_CLOSED.equalsIgnoreCase( poLineItem.getStatus() ) && !PURCHASE_ORDER_STATUS_CANCELLED.equalsIgnoreCase( poLineItem.getStatus() ) && !PURCHASE_ORDER_STATUS_NEW.equalsIgnoreCase( poLineItem.getStatus() ) && ( poLineItem.getRequestedQuantity() > poLineItem.getPreviouslyShipppedQty() ) )
			{
				recevingLineItem = new ReceivingLineItem();
				recevingLineItem.setSelected( false );
				recevingLineItem.setPurchaseOrderLineItem_Id( poLineItem.getPurchaseOrderLineItem_Id() );
				recevingLineItem.setPurchaseOrderLineItemNumber( poLineItem.getPurchaseOrderLineItemNumber() );
				recevingLineItem.setProcessStepId( poLineItem.getProcessStepId() );
				recevingLineItem.setPartNumber( poLineItem.getPartNumber() );
				recevingLineItem.setProcessName( poLineItem.getProcessName() );
				recevingLineItem.setPackageType( poLineItem.getPackageType() );
				recevingLineItem.setDescription( poLineItem.getDescription());
				recevingLineItem.setRequestedDate( poLineItem.getRequestedDate() );
				recevingLineItem.setRequestedQuantity( poLineItem.getRequestedQuantity() );
				recevingLineItem.setPreviouslyShipppedQty( poLineItem.getPreviouslyShipppedQty() );
				recevingLineItem.setShippedQty( 0l );
                recevingLineItem.setOrgShippedQty( 0l );
				recevingLineItem.setUnitCost( poLineItem.getUnitCost() );
				recevingLineItem.setExtendedCost( 0.0 );
				
				lineItemList.add( recevingLineItem ); 
			}
		}
		
		purchaseOrderReceiptForm.setReceivingLineItemList( lineItemList );
	}
}
